package cn.com.shbs.Controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import cn.com.shbs.Config.CommonUtil;
import cn.com.shbs.Config.Page;
import cn.com.shbs.Config.PasswordHelper;
import cn.com.shbs.Dao.LoginUserDao.LoginUserDao;
import cn.com.shbs.Dao.ShopInfoDao.BaseRoleInfoDao;
import cn.com.shbs.Dao.ShopInfoDao.ShopInfoDao;
import cn.com.shbs.Dao.ShopInfoDao.StoreRoleInfoDao;
import cn.com.shbs.Service.ShopInfoService.IShopInfoService;
import cn.com.shbs.Service.UserInfoService.IUserInfoService;
import cn.com.shbs.Service.code.SerialNumberGeneratorService;
import cn.com.shbs.model.BaseRole;
import cn.com.shbs.model.LoginUser;
import cn.com.shbs.model.Store;
import cn.com.shbs.model.StoreRole;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@RestController
@RequestMapping("API/")
public class ShopInfoController {

	static Logger logger = LoggerFactory.getLogger(ShopInfoController.class);

	@Autowired
	LoginUserDao loginUserDao;
	
    @Autowired
    public StoreRoleInfoDao storeroleInfoDao; //店面角色
    
    @Autowired
    public BaseRoleInfoDao baseroleInfoDao; //角色基本表
    
    @Autowired
    ShopInfoDao shopInfoDao ; //店面信息表
	
	@Autowired
	IShopInfoService shopInfoService;

	@Autowired
	SerialNumberGeneratorService serialNumberGeneratorService;

	@Autowired
	IUserInfoService userInfoService;

	/**
	 * 店铺添加
	 */

	@RequestMapping(value = "/addShopInfo")
	public Object addShopInfo(HttpServletRequest request, HttpServletResponse response,
			LoginUser userInfo,
			Store store
			) throws Exception {
		
		
		logger.info("ShopInfoController.addShopInfo()方法开始");

		
		String message = "success";
		JSONObject result=new JSONObject();
		
		
		//生成店铺ID
		String shopcodes= CommonUtil.PREFIX_STORE+serialNumberGeneratorService.getCodeGeneratorstr(CommonUtil.STEPNUM,CommonUtil.STORE_TYPE);;
		store.setStore_code(shopcodes);
		

		try {
			
			//判断当前用户是否存在
			LoginUser us=null;
            //根据登录名查询是否唯一
             us =  loginUserDao.findByloginname(userInfo.getLogin_name());
            if (us!=null){
                message="nameexit";
                result.put("message",message);
                return result;
            }

            //不存在设置用户参数
            PasswordHelper helper=new PasswordHelper();
            helper.encryptPassword(userInfo);
            byte State = 0;
            //userInfo.setCreattime(CommonUtil.getNowTime_tamp());
            String user_code=  CommonUtil.PREFIX_SYSUSER+serialNumberGeneratorService.getCodeGeneratorstr(CommonUtil.STEPNUM,CommonUtil.SYSUSER_TYPE);
            userInfo.setUser_code(user_code);
            store.setUser_code(user_code);
			shopInfoService.addstroeinfos(store,userInfo);
			
			message = "success";
			
		} catch (Exception e) {
			logger.error("ShopInfoController.addShopInfo()方法异常", e);
			message = "error";
			e.printStackTrace();
		}
		
		result.put("message",message);
		result.put("store_code",shopcodes);
	     return result.toString();

	}
	
	
	
	
	/**
	 * 店铺更新
	 */
	@RequestMapping(value = "/UpdateShop")
	public Object UpdateShop(HttpServletRequest request, HttpServletResponse response,
			Store store
			) throws Exception {
		
		
		logger.info("ShopInfoController.UpdateShop()方法开始");
		
		String message = "success";
		JSONObject result=new JSONObject();
		
		System.out.print(store.getStore_code());

		try {
			
			shopInfoDao.UpdateShop(store);
		
			message = "success";
			
		} catch (Exception e) {
			logger.error("ShopInfoController.UpdateShop()方法异常", e);
			message = "error";
			e.printStackTrace();
		}
		
		result.put("message",message);
		result.put("store_code",store.getStore_code());
	     return result.toString();

	}
	
	/**
	 * 店铺查询
	 */
	@RequestMapping(value = "/FindShopByName")
	public Object FindShopByName(HttpServletRequest request, HttpServletResponse response,
			Store store,
			Page page
			
			) throws Exception {
		
		
		logger.info("ShopInfoController.FindShopByName()方法开始");
		
		String message = "success";
		JSONObject result=new JSONObject();
		
		List<Store>  store_list = new ArrayList<Store>();
		HashMap map=new HashMap();

		try {
			
			
			int total=shopInfoDao.FindShopByNameNums(store);
			page.setTotalCount(total);
			map.put("pagestart", page.getStartCount());
			map.put("limit",10);
			map.put("store_name",store.getStore_name());
			store_list =	shopInfoDao.FindShopByName(map);
		
			message = "success";
			
		} catch (Exception e) {
			logger.error("ShopInfoController.FindShopByName()方法异常", e);
			message = "error";
			e.printStackTrace();
		}
		
		result.put("message",message);
		result.put("store_list",store_list);
		result.put("page", page);
	     return result.toString();

	}
	
	
	
	

}
